#include"my.h"

//退出功能 
void dealExit(int cfd)
{
	//封包
	MSG m;
	m.type = E;

	//发送给服务器
	if(write(cfd,&m,sizeof(m))<0)
	{
		perror("write");
		close(cfd);
		exit(-1);
	}
	memset(&m,0,sizeof(m));

	//接收服务器回发的数据包
	if(read(cfd,&m,sizeof(m))<=0)
	{
		perror("read");
		close(cfd);
		exit(-1);
	}
	//显示
	puts(m.data);
}

//处理查询功能
void dealQuery(int cfd,MSG *pm)
{
	//封包 
	pm->type = Q;

	//循环查询单词
	while(1)
	{
		puts("请输入要查询的单词(按#结束)");
		scanf("%s",pm->data);

		//判断查询是否结束
		if(!strcmp(pm->data,"#"))
		{
			break;
		}

		//发送给服务器
		if(write(cfd,pm,sizeof(MSG))<0)
		{
			perror("write");
			close(cfd);
			exit(-1);
		}
		memset(pm->data,0,sizeof(pm->data));

		//接收服务器回发的数据包
		if(read(cfd,pm,sizeof(MSG))<=0)
		{
			perror("read");
			close(cfd);
			exit(-1);
		}
		//显示
		puts(pm->data);

	}
}

//查看背词本
void dealHistory(int cfd,MSG *pm)
{
	//封包 
	pm->type = H;

	//发送 
	if(write(cfd,pm,sizeof(MSG))<0)
	{
		perror("write");
		close(cfd);
		exit(-1);
	}

	int count=0;

	//循环接收
	while(1)
	{
		//接收查询记录
		if(read(cfd,pm,sizeof(MSG))<=0)
		{
			perror("read");
			close(cfd);
			exit(-1);
		}

		//判断是否为结束标记 sendOver
		if(!strcmp(pm->data,"sendOver"))
		{
			break;
		}

		//显示
		printf("%18s   ",pm->data);
		
		memset(pm->data,0,sizeof(pm->data));

		if((count+1)%3==0)//0 1 2\n    3 4 5\n
		{
			putchar('\n');
		}

		count++;

	}
}
//显示二级菜单
void show(int cfd,MSG *pm)
{
	int choose;
	while(1)
	{
		puts("*******************************");
		puts("1.查单词   2. 背词本     3.退出");
		puts("*******************************");

		scanf("%d",&choose);

		switch(choose)
		{
		case 1:
			dealQuery(cfd,pm);
			break;
		case 2:
			dealHistory(cfd,pm);
			break;
		case 3:
			dealExit(cfd);
			exit(0);
		default:
			puts("choose error");
			break;
		}

		sleep(2);
		system("clear");
	}
}

void dealLogin(int cfd)
{
	//封包
	MSG m;
	m.type = L;

	puts("请输入要登录的用户名 密码");
	//输入帐号 密码
	scanf("%s%s",m.name,m.data);

	//发送给服务器
	if(write(cfd,&m,sizeof(m))<0)
	{
		perror("write");
		close(cfd);
		exit(-1);
	}
	memset(&m,0,sizeof(m));

	//接收服务器回发的数据包
	if(read(cfd,&m,sizeof(m))<=0)
	{
		perror("read");
		close(cfd);
		exit(-1);
	}

	//弹出二级菜单
	if(!strcmp(m.data,"登录成功"))
	{
		//显示菜单 
		show(cfd,&m);
	}
	else 
	{
		puts(m.data);
	}
}


void dealRegister(int cfd)
{
	//封包
	MSG m;
	m.type = R;

	puts("请输入 用户名 密码");
	//输入帐号 密码
	scanf("%s%s",m.name,m.data);

	//发送给服务器
	if(write(cfd,&m,sizeof(m))<0)
	{
		perror("write");
		close(cfd);
		exit(-1);
	}
	memset(&m,0,sizeof(m));

	//接收服务器回发的数据包
	if(read(cfd,&m,sizeof(m))<=0)
	{
		perror("read");
		close(cfd);
		exit(-1);
	}
	//显示
	puts(m.data);
}

void showMenu(int cfd)
{
	int choose;
	while(1)
	{
		puts("**********************************");
		puts(" 1. 注册       2. 登录     3. 退出");
		puts("**********************************");

		scanf("%d",&choose);

		switch(choose)
		{
		case 1:
			dealRegister(cfd);
			break;
		case 2:
			dealLogin(cfd);
			break;
		case 3:
			dealExit(cfd);
			exit(0);
		default:
			puts("choose error");
		}
		sleep(2);
		system("clear");//清屏
	}
}
// ./c  server ip  server port
int main(int argc,const char*argv[])// ./c 192.168.0.11 2025
{

	//错误处理 
	if(argc!=3)
	{
		printf("%s serverIp  server Port",argv[0]);
		exit(-1);
	}

	//1.创建套接字
	int cfd = socket(AF_INET,SOCK_STREAM,0);
	if(cfd<0)
	{
		perror("socket");
		exit(-1);
	}

	//2.发起三次握手连接请求
	struct sockaddr_in serverAddr;
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port  = htons(atoi(argv[2]));//服务器端口  argv[2]
	serverAddr.sin_addr.s_addr = inet_addr(argv[1]);//服务器ip
	int ret = connect(cfd,(struct sockaddr*)&serverAddr,sizeof(serverAddr));//成功返回标志三次握手完成
	if(ret<0)
	{
		perror("connect");
		close(cfd);
		exit(-1);
	}

	//显示一级菜单
	showMenu(cfd);
}
